<?php

defined('SYSPATH') or die('No direct access allowed.');

/**
 * Service Customer
 */
class Model_Service_Customer extends Domain_Dao_Customer {

    public $_rep;

    /**
     * 
     */
    public function __construct() {
        $o_repository_manager = new Domain_Repositories_RepositoryManager;
        $this->_rep = $o_repository_manager->getCustomerRepository();
    }

    /**
     *
     * @return <type>
     */
    public function load($id) {
        return $this->_rep->load($id);
    }

    /**
     *
     * @param <type> $offset
     * @param <type> $limit
     * @param <type> $filter
     * @param <type> $sort
     * @param <type> $count
     * @return <type>
     */
    public function load_all($is_count = false, $offset = null, $limit = null, $filter = null, $sort = null) {
        return $this->_rep->load_all($offset, $limit, $filter, $sort, $is_count);
    }

    /**
     *
     * @param Domain_Entities_User $o_entity
     * @return <type> 
     */
    public function insert($o_entity) {
        return $this->_rep->insert_by($o_entity);
    }

    /**
     *
     * @param Domain_Entities_User $o_entity
     * @return <type>
     */
    public function update($o_entity) {
        return $this->_rep->update_by($o_entity);
    }

    /**
     *
     * @param <type> $id
     * @return <type> 
     */
    public function delete($id) {
        return $this->_rep->delete_by($id);
    }

    public function get($cmnd) {
        $this->_rep->clear();
        $this->_rep->builder_select('cmnd', array('where' => '='), $cmnd);
        return $this->_rep->query();
    }

    public function getall($search, $date, $count = false, $offset = null, $limit = null, $sort = null) {
        $this->_rep->clear();
        $this->_rep->builder_select(DB::expr('DATE(created)'), array('where' => '=', 'limit' => $limit, 'offset' => $offset), $date);
        $this->_rep->build_sort($sort);
        if (!empty($search)) {
            $this->_rep->where_open();
            $this->_rep->builder_select('fullname', array('where' => 'like'), $search);
            $this->_rep->builder_select('email', array('or where' => 'like'), $search);
            $this->_rep->builder_select('phone', array('or where' => 'like'), $search);
            $this->_rep->builder_select('address', array('or where' => 'like'), $search);
            $this->_rep->builder_select('cmnd', array('or where' => 'like'), $search);
            $this->_rep->where_close();
        }

        if ($count) {
            return $this->_rep->get_count();
        }
        return $this->_rep->query_all();
    }

    public function getall2($search, $date, $count = false, $offset = null, $limit = null, $sort = null) {
        $this->_rep->clear();

        $this->_rep->build_sort($sort);
        if (!empty($search)) {
            $this->_rep->where_open();
            $this->_rep->builder_select($this->_rep->getModel() . '.fullname', array('where' => 'like'), $search);
            $this->_rep->builder_select($this->_rep->getModel() . '.email', array('or where' => 'like'), $search);
            $this->_rep->builder_select($this->_rep->getModel() . '.phone', array('or where' => 'like'), $search);
            $this->_rep->builder_select($this->_rep->getModel() . '.address', array('or where' => 'like'), $search);
            $this->_rep->builder_select($this->_rep->getModel() . '.cmnd', array('or where' => 'like'), $search);
            $this->_rep->where_close();
        }

        $s_level = new Model_Service_Level();
        $this->_rep->join($s_level->_rep->table_name(), 'INNER');
        $this->_rep->on($s_level->_rep->table_name() . '.customer_id', '=', $this->_rep->getModel() . '.id');
        $this->_rep->builder_select(DB::expr('DATE(' . $s_level->_rep->get_table_name() . '.created)'), array('where' => '='), $date);

        if ($count) {
            $this->_rep->select(DB::expr("COUNT(distinct " . $this->_rep->getModelSelect() . '.id) as __count'));
            return $this->_rep->query(array('id' => 'id'))->get_data()->get__count();
        }

        $this->_rep->builder_select($this->_rep->getModel() . '.created', array('limit' => $limit, 'offset' => $offset, 'group by' => null));
        return $this->_rep->query_all();
    }

    public function gettop($date, $level, $count = false, $offset = 0, $limit = 10) {
        $this->_rep->clear();
        $this->_rep->builder_select($this->_rep->getModel() . '.created', array('limit' => $limit, 'offset' => $offset));

        $this->_rep->builder_select($this->_rep->getModel() . '.id', array('group by' => null));

        $s_level = new Model_Service_Level();
        $this->_rep->join($s_level->_rep->table_name(), 'INNER');
        $this->_rep->on($s_level->_rep->table_name() . '.customer_id', '=', $this->_rep->getModel() . '.id');

        $datex = DateTime::createFromFormat('Y-m-d', $date);
        $datex->modify('+7 days');
        $date7 = $datex->format('Y-m-d');

        $this->_rep->builder_select(DB::expr('DATE(' . $s_level->_rep->get_table_name() . '.created)'), array('where' => '>='), $date);
        $this->_rep->builder_select(DB::expr('DATE(' . $s_level->_rep->get_table_name() . '.created)'), array('where' => '<'), $date7);

        $s_point = new Model_Service_Point();
        $this->_rep->join($s_point->_rep->table_name());
        $this->_rep->on($s_point->_rep->table_name() . '.level_id', '=', $s_level->_rep->table_name() . '.id');
        $this->_rep->builder_select($s_point->_rep->table_name() . '.level', array('where' => '='), $level);

        $this->_rep->builder_select($s_point->_rep->table_name() . '.point', array('order by' => 'desc'));

        $this->_rep->select(DB::expr($s_point->_rep->get_table_name() . '.point as __point'));
        $this->_rep->select(DB::expr($s_point->_rep->get_table_name() . '.time as __time'));
        $this->_rep->select(DB::expr($s_point->_rep->get_table_name() . '.created as __created'));

        if ($count) {
            return $this->_rep->get_count();
        }
        return $this->_rep->query_all();
    }

    public function gettopallweek($event_id, $offset = 0, $limit = 10) {
        $this->_rep->clear();
        $this->_rep->builder_select($this->_rep->getModel() . '.created', array('limit' => $limit, 'offset' => $offset));

        $s_level = new Model_Service_CustomerPoint();
        $this->_rep->join($s_level->_rep->table_name(), 'INNER');
        $this->_rep->on($s_level->_rep->table_name() . '.customer_id', '=', $this->_rep->getModel() . '.id');


        $this->_rep->select(DB::expr("(" . $s_level->_rep->get_table_name() . '.week1 '
                        . '+ ' . $s_level->_rep->get_table_name() . '.week2'
                        . ' + ' . $s_level->_rep->get_table_name() . '.week3) as __point'));
        $this->_rep->builder_select($s_level->_rep->table_name() . '.event_id', array('where' => '='), $event_id);
        $this->_rep->builder_select(DB::expr('__point'), array('order by' => 'desc'));
        $this->_rep->builder_select($this->_rep->getModel() . ".fullname", array('order by' => 'desc'));

        return $this->_rep->query_all();
    }

    public function getweek($event_id, $week, $count = false, $offset = 0, $limit = 10) {
        $this->_rep->clear();
        $this->_rep->builder_select($this->_rep->getModel() . '.created', array('limit' => $limit, 'offset' => $offset));

        $s_level = new Model_Service_CustomerPoint();
        $this->_rep->join($s_level->_rep->table_name(), 'INNER');
        $this->_rep->on($s_level->_rep->table_name() . '.customer_id', '=', $this->_rep->getModel() . '.id');

        if ($week == 1) {
            $this->_rep->select(DB::expr($s_level->_rep->get_table_name() . '.week1  as __point'));
            $this->_rep->builder_select($s_level->_rep->table_name() . '.week1', array('where' => '>'), 0);
        } else if ($week == 2) {
            $this->_rep->select(DB::expr($s_level->_rep->get_table_name() . '.week2  as __point'));
            $this->_rep->builder_select($s_level->_rep->table_name() . '.week2', array('where' => '>'), 0);
        } else {
            $this->_rep->select(DB::expr($s_level->_rep->get_table_name() . '.week3  as __point'));
            $this->_rep->builder_select($s_level->_rep->table_name() . '.week3', array('where' => '>'), 0);
        }

        $this->_rep->builder_select($s_level->_rep->table_name() . '.event_id', array('where' => '='), $event_id);



        if ($count) {
            return $this->_rep->get_count();
        }
        $this->_rep->builder_select(DB::expr('__point'), array('order by' => 'desc'));
        $this->_rep->builder_select($this->_rep->getModel() . ".fullname", array('order by' => 'desc'));
        return $this->_rep->query_all();
    }

    public function getmaxuser($customer_id, $date, $level) {
        $this->_rep->clear();

        $this->_rep->builder_select($this->_rep->getModel() . '.id', array('where' => '='), $customer_id);

        $s_level = new Model_Service_Level();
        $this->_rep->join($s_level->_rep->table_name(), 'INNER');
        $this->_rep->on($s_level->_rep->table_name() . '.customer_id', '=', $this->_rep->getModel() . '.id');

        $datex = DateTime::createFromFormat('Y-m-d', $date);
        $datex->modify('+7 days');
        $date7 = $datex->format('Y-m-d');

        $this->_rep->builder_select(DB::expr('DATE(' . $s_level->_rep->get_table_name() . '.created)'), array('where' => '>='), $date);
        $this->_rep->builder_select(DB::expr('DATE(' . $s_level->_rep->get_table_name() . '.created)'), array('where' => '<'), $date7);

        $s_point = new Model_Service_Point();
        $this->_rep->join($s_point->_rep->table_name());
        $this->_rep->on($s_point->_rep->table_name() . '.level_id', '=', $s_level->_rep->table_name() . '.id');
        $this->_rep->builder_select($s_point->_rep->table_name() . '.level', array('where' => '='), $level);

        $this->_rep->builder_select($s_point->_rep->table_name() . '.point', array('order by' => 'desc'));

        $this->_rep->select(DB::expr($s_point->_rep->get_table_name() . '.point as __point'));
        $this->_rep->select(DB::expr($s_point->_rep->get_table_name() . '.time as __time'));
        $this->_rep->select(DB::expr($s_point->_rep->get_table_name() . '.created as __created'));

        return $this->_rep->query();
    }

    public function getallpoint($customer_id,$date) {
        $this->_rep->clear();

        $this->_rep->builder_select($this->_rep->getModel() . '.id', array('where' => '='), $customer_id);

        $s_level = new Model_Service_Level();
        $this->_rep->join($s_level->_rep->table_name(), 'INNER');
        $this->_rep->on($s_level->_rep->table_name() . '.customer_id', '=', $this->_rep->getModel() . '.id');

        
        $datex = DateTime::createFromFormat('Y-m-d', $date);
        $datex->modify('+7 days');
        $date7 = $datex->format('Y-m-d');

        $this->_rep->builder_select(DB::expr('DATE(' . $s_level->_rep->get_table_name() . '.created)'), array('where' => '>='), $date);
        $this->_rep->builder_select(DB::expr('DATE(' . $s_level->_rep->get_table_name() . '.created)'), array('where' => '<'), $date7);
        
        $s_point = new Model_Service_Point();
        $this->_rep->join($s_point->_rep->table_name());
        $this->_rep->on($s_point->_rep->table_name() . '.level_id', '=', $s_level->_rep->table_name() . '.id');

        $this->_rep->builder_select($s_point->_rep->table_name() . '.point', array('order by' => 'desc'));

        $this->_rep->select(DB::expr($s_point->_rep->get_table_name() . '.point as __point'));
        $this->_rep->select(DB::expr($s_point->_rep->get_table_name() . '.time as __time'));
        $this->_rep->select(DB::expr($s_point->_rep->get_table_name() . '.created as __created'));

        return $this->_rep->query_all();
    }

}

?>
